﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoucreGame.DTO;
using SoucreGame.DTO.Tru;

namespace SoucreGame.BUS.CBusiTower
{
    class CBusiLinh : CBusiNhanVat
    {
        public Vector2 toaDoLinh { get; set; }
        int toaDoX=10;//khoang cách X Lính so với Nhà Lính khi dc khởi tọa
        int toaDoY = 5;//khoang cách Y Lính so với Nhà Lính khi dc khởi tọa
        /// <Hàm Khởi tạo>
        /// 
        /// </summary>
        /// <param name="conten"></param>
        /// <param name="gameTime"></param>
        /// <param name="sp"></param>
        #region
        public CBusiLinh(ContentManager conten, GameTime gameTime, SpriteBatch sp)
            : base(conten, gameTime, sp)
        {

            this.NhanVat = new CInfoLinh(conten, null, sp);
            LoadDuLieu();

        }
        public CBusiLinh()
            : base()
        {

            this.NhanVat = new CInfoLinh(this.Content, null, this.SpriteBatch);
            LoadDuLieu();

        }
        private void LoadDuLieu()
        {
            this.NhanVat.Ten = NhanVat.Ten;
            this.NhanVat.Mau = NhanVat.Mau;
            this.NhanVat.TocDoTanCong = NhanVat.TocDoTanCong;
            this.NhanVat.ToaDoHienTai = NhanVat.ToaDoHienTai;
            this.NhanVat.PhamViTC = NhanVat.PhamViTC;
            this.NhanVat.SucTanCong = NhanVat.SucTanCong;
            this.NhanVat.SucPhongThu = NhanVat.SucPhongThu;
            this.NhanVat.TrangThai = NhanVat.TrangThai;
            this.NhanVat.SttAnhBatDau = NhanVat.SttAnhBatDau;
            this.NhanVat.SoLuongAnhChet = NhanVat.SoLuongAnhChet;
            this.NhanVat.SoLuongAnhTanCong = NhanVat.SoLuongAnhTanCong;
            this.NhanVat.SoLuongAnhDiChuyen = NhanVat.SoLuongAnhDiChuyen;
            this.thoiGianChet = 150;
            
            if (this.Content != null)
            {
                this.NhanVat.AnhNhanVat = NhanVat.AnhNhanVat;
                this.NhanVat.ViTriAnh = NhanVat.ViTriAnh;
                toaDoLinh = new Vector2(200, 200);
            }

        }
        #endregion

        /// <Hàm Update Lính của Trụ Nhà Lính>
        /// 
        /// </summary>
        #region
        int framTimeLine = 1; int sodi = 1; bool kt=true;
        public new void Update(GameTime gameTime,int ViTri)
        {
            framTimeLine +=(int) gameTime.ElapsedGameTime.TotalMilliseconds;
            if (kt == true)
            {
                if(ViTri==1)
                    DiChuyen2();
                if (ViTri == 2)
                    DiChuyen();
                if (ViTri == 3)
                    DiChuyen3();
                
            }
          // Chet();
          TanCong();
            
        }
        private void Chet()
        {
            if (framTimeLine >= 200)
            {
                NhanVat.SttAnhBatDau++;
                framTimeLine = 0;
                if (NhanVat.SttAnhBatDau >= NhanVat.SoLuongAnhChet)
                    NhanVat.SttAnhBatDau = NhanVat.SoLuongAnhChet;
            }
        }
        private void TanCong()
        {
            if (framTimeLine >= 230)
            {
                NhanVat.SttAnhBatDau++;
                framTimeLine = 0;
                if (NhanVat.SttAnhBatDau >= NhanVat.SoLuongAnhTanCong)
                    NhanVat.SttAnhBatDau = 1;
            }
        }
        private void DiChuyen()
        {
            if (framTimeLine >= 100)
            {
                sodi++;
                NhanVat.SttAnhBatDau++;
                framTimeLine = 0;
                toaDoLinh = new Vector2(toaDoLinh.X + toaDoX, toaDoLinh.Y+toaDoY);
                if (NhanVat.SttAnhBatDau >= NhanVat.SoLuongAnhDiChuyen)
                    NhanVat.SttAnhBatDau = 1;
                if (sodi == 10)
                    kt = false;
            }
        }
        private void DiChuyen2()
        {
            if (framTimeLine >= 100)
            {
                sodi++;
                NhanVat.SttAnhBatDau++;
                framTimeLine = 0;
                toaDoLinh = new Vector2(toaDoLinh.X + toaDoX, toaDoLinh.Y-toaDoY);
                if (NhanVat.SttAnhBatDau >= NhanVat.SoLuongAnhDiChuyen)
                    NhanVat.SttAnhBatDau = 1;
                if (sodi == 10)
                    kt = false;
            }
        }
        private void DiChuyen3()
        {
            if (framTimeLine >= 100)
            {
                sodi++;
                NhanVat.SttAnhBatDau++;
                framTimeLine = 0;
                toaDoLinh = new Vector2(toaDoLinh.X + toaDoX, toaDoLinh.Y);
                if (NhanVat.SttAnhBatDau >= NhanVat.SoLuongAnhDiChuyen)
                    NhanVat.SttAnhBatDau = 1;
                if (sodi == 10)
                    kt = false;
            }
        }
        #endregion

        public new void Draw(GameTime gameTime)
        {
            switch (Hanhdong)
            {
                case chon.tancong:
                    {
                        this.SpriteBatch.Draw(this.NhanVat.AnhNhanVat, this.toaDoLinh, this.NhanVat.ViTriAnh[this.NhanVat.ChuoiTanCong + this.NhanVat.SttAnhBatDau], Color.White);
                        break;
                    }
                case chon.chet:
                    {
                        this.SpriteBatch.Draw(this.NhanVat.AnhNhanVat, this.toaDoLinh, this.NhanVat.ViTriAnh[this.NhanVat.ChuoiChet + this.NhanVat.SttAnhBatDau], Color.White); ;
                        break;
                    }
                case chon.dungyen:
                    {
                        this.SpriteBatch.Draw(this.NhanVat.AnhNhanVat, this.toaDoLinh, this.NhanVat.ViTriAnh[this.NhanVat.ChuoiDungYen + this.NhanVat.SttAnhBatDau], Color.White);
                        break;
                    }
                case chon.dichuyen:
                    {
                        this.SpriteBatch.Draw(this.NhanVat.AnhNhanVat, this.toaDoLinh, this.NhanVat.ViTriAnh[this.NhanVat.ChuoiDiChuyen + this.NhanVat.SttAnhBatDau], Color.White);
                        break;
                    }
            }
        }
    }
}
